#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Purpose: 
# Created: 04/22/2009
# $Id $
# Copyright (c) 2008,2009 by University College London
# Authors:
# Didrik Pinte --<dpinte@dipole-consulting.com>
#
# This program is free software under the GPL (>=v2)
# Read the file COPYING coming with HydroPlatform for details.
'''
The About Box based on Thuban About box
'''

__version__ = "$Revision: 2825 $"
# $Source$
# $Id: about.py 2825 2008-01-31 13:58:23Z bernhard $

import platform
import webbrowser
import wx

from Thuban import _, get_internal_encoding
from Thuban.Model.resource import gdal_support_status
from Thuban.UI.extensionregistry import ext_registry

from hydroplatform.version import versions

class About(wx.Dialog):
    '''
    About dialog class
    '''

    def __init__(self, parent):
        wx.Dialog.__init__(self, parent, -1, _("About HydroPlatform"),
            style = wx.DEFAULT_DIALOG_STYLE
                  | wx.SYSTEM_MENU
                  | wx.MINIMIZE_BOX
                  | wx.MAXIMIZE_BOX
                  | wx.RESIZE_BORDER,
            size = (400, 250))

        # Note: The source code is in ASCII, so we escape some
        # characters to get byte strings in latin1.
        maintainers = 'Hossam Almeer (2010-current)',
        lead_developer = 'Hossam Almeer (2010=current), Didrik Pinte (2008 - 2009)'
        developers = [ 'Hossam Almeer (2010-current),Didrik Pinte (2008, 2009)']
        translators = [ ]
        other_contributors = [ ]
        dyn_modules = [ ('wxPython', versions['wxPython']),
                        ('Python',   versions['python']),
                        ('PySQLite', versions['pysqlite']),
                        ('SQLite',  versions['sqlite']),
                        ('GDAL', versions.get('gdal', _('- not available'))),
                        ('psycopg', versions.get('psycopg',
                                                 _('- not available')))]
        direct_modules = [ \
                ('GTK',      versions.get('gtk', _('- not available'))),
                ('proj',     versions['proj']) ]

        text = 'HydroPlatform %s\n\n' % versions['hydroplatform-long']

        text += _('Currently using:\n')

        text += 'Thuban %s\n\n' % versions['thuban-long']
        
        for name, version in dyn_modules:
            text += '\t%s %s\n' % (name, version)
        text += '\n'

        text += _('\tInternal encoding: %s\n') % get_internal_encoding()
        text += '\n'

        if gdal_support_status:
            text += gdal_support_status + "\n\n"

        text += _('Compiled for:\n')

        for name, version in direct_modules:
            text += '\t%s %s\n' % (name, version)
        text += '\n'

        text += _('Extensions:\n')
        if ext_registry:
            for ext in ext_registry:
                text += '\t%s %s\n' % (ext.name, ext.version)
        else:
            text += _('\tNone registered.\n')
        text += '\n'

        text += _('Maintainers:\n')
        for name in maintainers:
            text += '\t%s\n' % name
        text += '\n'

        text += _('Lead Developer:\n')
        text += '\t%s\n\n' % lead_developer

        text += _('Developers:\n')
        for name in developers:
            text += '\t%s\n' % name
        text += '\n'

        text += _('Translators:\n')
        for lang, name in translators:
            text += '\t%s: %s\n' % (lang, name)
        text += '\n'

        text += _('Other Contributors:\n')
        for name in other_contributors:
            text += '\t%s\n' % name
        text += '\n'

        text += \
            _("Questions and comments can be sent to the following addresses:\n"
            "\tGeneral :\n\t\t<info@hydroplatform.org>\n"
            )

        text += '\n\n'

        text += _("Details on the registered extensions:\n\n")

        if ext_registry:
            for ext in ext_registry:
                text += '%s %s:\n' % (ext.name, ext.version)
                text += _('Copyright %s\n') % ext.copyright
                text += _('Authors:\n')
                for author in ext.authors:
                    text += '\t%s\n' % author
                text += ext.desc
                text += '\n'
                text += 'Status: %s' % ext.status
                text += '\n\n'
        else:
            text += _('\tNone registered.\n')

        self.text = text
        text_style = wx.ALIGN_LEFT \
                      |wx.TE_READONLY | wx.NO_BORDER \
                      |wx.TE_MULTILINE | wx.TE_LINEWRAP |\
                      wx.TE_NO_VSCROLL
        '''
        FIXME : the following dirty trick is because of 
        wxPython 2.6 rendering the text as password when using
        the TE_AUTO_URL on windows ...
        '''
        if platform.system() is not "Windows":
            test_style = text_style | wx.TE_AUTO_URL
            
        text_title = wx.TextCtrl(self, -1,
            _('''
HydroPlatform is a generic user interface and 
data manager for network models.
www.hydroplatform.org \n
 
Copyright 2008-2009 University College London
HydroPlatform is licensed under the GNU GPL v>=2
HydroPlatform uses Thuban, a geographic data viewer"'''),
                                  style= text_style)        
        text_title.SetBackgroundColour(self.GetBackgroundColour())
        text_title.SetSize((400, 200))
        text_title.SetSizeHints(400, 200)
        
        text_box = wx.TextCtrl(self, -1, text,
            style=wx.TE_READONLY|wx.TE_MULTILINE|wx.TE_LINEWRAP)
        wdw, wdh = (400, 150)
        text_box.SetSizeHints(wdw, wdh)
        text_box.SetSize((wdw, wdh))

        button_close = wx.Button(self, wx.ID_CANCEL, _("Close"))
        button_close.SetDefault()

        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(text_title, 0, wx.ALL|wx.EXPAND|wx.ADJUST_MINSIZE, 10)
        sizer.Add(text_box, 1, wx.ALL|wx.EXPAND, 10)
        sizer.Add(button_close, 0, wx.ALL|wx.ALIGN_RIGHT, 10)

        self.SetAutoLayout(True)
        self.SetSizer(sizer)
        sizer.Fit(self)
        sizer.SetSizeHints(self)
        self.Layout()

        self.Bind(wx.EVT_BUTTON, self.on_cancel, id=wx.ID_CANCEL)
        self.Bind(wx.EVT_TEXT_URL, self.on_urlselected)

        
    def on_urlselected(self, event):
        '''
        When a url is clicked on in the text_title TextCtrl
        '''
        if event.GetMouseEvent().LeftUp():
            text = event.GetEventObject().GetValue()
            url  = text[event.GetURLStart():event.GetURLEnd()]
            webbrowser.open(url)
        
    def on_cancel(self, event):
        '''
        Closes the dialog
        '''
        self.EndModal(wx.ID_CANCEL)
